# coding:utf-8


import sys
from analyze_text_pre import Text
from TextOne import Textone
from TextTwo import Texttwo
from conf import *
from conf1 import *
from loguru import logger


class AnalyzeText():
    def __init__(self, **params):
        self.html = params['html']
        self.database = params['database']
        self.config = params['config']
        self.config1 = params['config1']
        self.params1 = {'config': self.config,
                        'get_money': params['get_money'],
                        'data_source_id': params['data_source_id'],
                        'clear_solo': params['clear_solo']}
        self.type = params['type']

    def get_data_html(self):
        html_replace_one, html_replace_two = Text(**self.params1).read(self.html, self.database)
        return html_replace_one, html_replace_two

    def data_handle(self, html_replace_one, html_replace_two):
        type_sec = self.config1['type_sec'][self.type]
        if self.type != '3':
            out_dict = {key: '' if key != '投标要求' else [] for key in list(self.config1[type_sec].keys())}
        else:
            out_dict = {key: '' for key in list(self.config1['achivement_sec_common'].keys())}
            out_dict['第一中标候选人'] = []
            out_dict['第二中标候选人'] = []
            out_dict['第三中标候选人'] = []
        if self.type == '3':
            result_one = Textone(**self.params1).part_one_text(html_replace_one)
            out_dict['第一中标候选人'] = result_one['第一中标候选人']
            out_dict['第二中标候选人'] = result_one['第二中标候选人']
            out_dict['第三中标候选人'] = result_one['第三中标候选人']
        result_two = Texttwo(**self.params1).part_two_text(html_replace_two)
        for key in result_two:
            if re.search('^第[一二三]中标候选人$',key):
                continue
            if key in result_two:
                out_dict[key] = result_two[key]
        return out_dict

    def main(self):
        html_replace_one, html_replace_two = self.get_data_html()
        end = self.data_handle(html_replace_one, html_replace_two)
        return end


if __name__ == '__main__':
    conn = pymysql.connect(
        host='192.168.0.160',
        port=3306,
        user='root',
        passwd='lubanjianye',
        charset='utf8',
        database='data_collection_server',
        cursorclass=pymysql.cursors.DictCursor)
    cursor = conn.cursor()
    args = {'conn': conn,
            'cursor': cursor}
    conf = Conf(**args)
    config = conf.main()
    conf1 = Conf1(**args)
    config1 = conf1.main()
    confunc = ConFunc(config)
    get_money = confunc.get_money
    clear_solo = confunc.clear_solo
    params = {'html': 'C:/Users/Administrator/Desktop/table_easy/table8.html',
              'database': False,
              'config': config,
              'config1': config1,
              'get_money': get_money,
              'clear_solo': clear_solo,
              'type':'3',
              'data_source_id': 1000075}
    print(AnalyzeText(**params).main())